//
//  LGFSegmentView.swift
//  Auscultation
//
//  Created by 来 on 2020/12/22.
//

import Foundation

class LGFSegmentView: UIView {
    
    var datas: [UILabel]!
    var selectColor: UIColor!
    var unSelectColor: UIColor!
    var sTextColor: UIColor!
    var unsTextColor: UIColor!
    
    var callIndex: ((Int) -> Void)!
    
    var dx: Bool = false
    
    var selectIndex: Int = 0
    var selectIndexs: [Int] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        datas = []
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setData(data: [String], font: UIFont, selectColor: UIColor, unSelectColor: UIColor, sTextColor: UIColor, unsTextColor: UIColor, dx: Bool) {
        self.dx = dx
        self.selectColor = selectColor
        self.unSelectColor = unSelectColor
        self.sTextColor = sTextColor
        self.unsTextColor = unsTextColor
        let width = self.lgf_Width / CGFloat(data.count)
        for (i, v) in data.enumerated() {
            let label = UILabel.init()
            label.text = v
            label.font = font
            label.textColor = unsTextColor
            label.isUserInteractionEnabled = true
            if i == 0 && !self.dx {
                label.backgroundColor = selectColor
            } else {
                label.backgroundColor = unSelectColor
            }
            label.textAlignment = .center
            label.lgf_AddTap(target: self, action: #selector(labelSelect(_:)))
            self.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.equalTo(CGFloat(i) * width)
                make.top.bottom.equalToSuperview()
                make.width.equalTo((i == data.count - 1) ? width : width - 1)
            }
            datas.append(label)
        }
    }
    
    @objc func labelSelect(_ sender: UITapGestureRecognizer) {
        if let label = sender.view as? UILabel {
            if dx {
                let index = datas.lgf_IndexOfObject(label)
                if selectIndexs.contains(index) {
                    selectIndexs.lgf_Remove(index)
                } else {
                    selectIndexs.append(index)
                }
                datas.forEach { (l) in
                    l.textColor = unsTextColor
                    l.backgroundColor = unSelectColor
                }
                selectIndexs.forEach { (i) in
                    datas[i].textColor = sTextColor
                    datas[i].backgroundColor = selectColor
                }
            } else {
                datas.forEach { (l) in
                    l.textColor = unsTextColor
                    l.backgroundColor = unSelectColor
                }
                label.textColor = sTextColor
                label.backgroundColor = selectColor
                selectIndex = datas.lgf_IndexOfObject(label)
                callIndex?(selectIndex)
            }
        }
    }
    
}
